﻿/*===================================================================================/
 * 
 * FILE: EnergyCell.cs
 * AUTH: Matthew Baum
 * DESC: Energy cells to heal the player!
 * 
 *==================================================================================*/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Homebound
{
    // Class definition
    class EnergyCell
    {
        // Members
        private static Texture2D texture;
        private static Vector2 origin;
        private static SoundEffect lifeUp;

        // Level to which this cell belongs
        public static ContentManager content;

        // Sine animation along Y
        private Vector2 basePosition;
        private float bounce;
       
        // Position
        public Vector2 position;

        // Heal amount
        public const int HEAL = 10;

        // Define circle for collisions
        public Circle BoundingCircle
        {
            get
            {
                return new Circle(position, Tile.Width / 3.0f);
            }
        }

        // Constructor
        public EnergyCell(Vector2 p)
        {
            // Set members
            position = basePosition = p;
        }

        // Load required content
        public static void loadContent( ContentManager cm )
        {
            // Set level
            content = cm;

            // Load content
            texture = content.Load<Texture2D>("Sprites/power_cell");
            origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
            lifeUp = content.Load<SoundEffect>("Sounds/GemCollected");
        }

        // Update energy cells
        public void update(GameTime gameTime)
        {
            // Bounce control constants
            const float BounceHeight = 0.18f;
            const float BounceRate = 3.0f;
            const float BounceSync = -0.75f;

            // Bounce along a sine curve over time.
            // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.            
            double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + position.X * BounceSync;
            bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;

            position = new Vector2(basePosition.X, basePosition.Y + bounce);
        }

        // Play sound when collected
        public void onCollected(Player p)
        {
            // Play sound FX
            lifeUp.Play();

            // Update player life
            p.updateLife(HEAL);
        }

        // Draw an Energy Cell
        public void draw( SpriteBatch spriteBatch )
        {
            spriteBatch.Draw(texture, position, null, Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
        }

    }
}
